#ifndef _GAME_SESSION_H_
#define _GAME_SESSION_H_

#include "IoSocketEx.h"
#include <string>
#include <map>
#include <set>
#include <vector>
#include "GeneralMessage.pb.h"
#include "MainMessage.pb.h"

class CGameSocket : public SGLib::CIoSocketEx
{
public:
    enum
    {
        E_GeneralList_PageSize = 20,

        E_Action_Other = 0,
        E_Action_Quality = 1,   // 传功
        E_Action_Evolution = 2, // 进化
    };

    CGameSocket(s32 id) : SGLib::CIoSocketEx(id), m_account("")
    {}
    virtual SGLib::CIoSocketEx* Clone(s32 id) { return new CGameSocket(id); }
    virtual void OnConnect(UserData &ud);
    virtual void OnConnectError(UserData &ud);
    virtual void OnClose();
    virtual void OnRecv(const byte *pkg, s32 len);

    void SendCommonMsg(com::future::threekingdoms::server::transport::protocol::CommonMessage &comMessage);
    void TryCostGoldIngot();
    void CostGoldIngot();

    void SendLogin();
    void HeartBeat();
    void GetGeneralList(int page, int pagesz);
    void GeneralEvolution(s64 gid, s64 eatid, com::future::threekingdoms::server::transport::protocol::GeneralBaseMsg &g);
    void GeneralQuality(s64 gid, std::vector<s64> &eatid, com::future::threekingdoms::server::transport::protocol::GeneralBaseMsg &g);
    void DrawGenerals();


protected:
    virtual void _Clean();
    virtual void  _ProcessRecvData();
    static void _timercall_CostGoldIngot(void *pData, s32 nDataLen);

private:
    void _LoginSuccess();
    void _WalletUpdate(com::future::threekingdoms::server::transport::protocol::CommonMessage &cm);
    void _RecvGeneralList(com::future::threekingdoms::server::transport::protocol::CommonMessage &cm);
    void _GeneralChangeResult(com::future::threekingdoms::server::transport::protocol::CommonMessage &cm);
    void _DrawGeneralList(com::future::threekingdoms::server::transport::protocol::CommonMessage &cm);

    void _RemoveCostGeneralList();

private:
    int m_curRecharge; // 本次充值金额
    int m_curUse; // 本次登录已消耗的元宝
    std::string m_account;
    bool m_hasLogin;
    u64 m_loginTime;
    int m_page;
    int m_goldIngot;

    int m_action; // 下一步的动作 
    std::set<s64> m_costGeneralList; // 传功和进化即将要消耗的武将

    // <moduleid, <id, General>>
    std::map<int, std::map<s64, com::future::threekingdoms::server::transport::protocol::GeneralBaseMsg> > m_generals;
    std::map<int, s64> m_generalIdx; // <moduleid, id> 候选操作武将列表
    std::map<s64, int> m_generalIdx2; // <id, modleid>
};

#endif

